package model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public final class Contacts implements ContactsInterface {

	private Map<String, Contact> contacts;

	public Contacts() {
		super();
		this.contacts = new HashMap<String, Contact>();
	}

	public Contacts(int number) {
		super();
		this.contacts = new HashMap<String, Contact>();

		for (int i = 0; i < number; i++) {
			Contact rContact = randomContact();
			this.contacts.put(this.makeKey(rContact), rContact);

		}

	}

	private String randomString(int len) {
		char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < len; i++) {
			char c = chars[random.nextInt(chars.length)];
			sb.append(c);
		}
		String output = sb.toString();
		return output;
	}

	private String randomNumbers(int len) {
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < len; i++) {
			char c = Integer.toString(random.nextInt(10)).charAt(0);
			sb.append(c);
		}
		String output = sb.toString();
		return output;
	}

	private Contact randomContact() {
		String fName = randomString(new Random().nextInt(7) + 3);
		String lName = randomString(new Random().nextInt(7) + 3);
		String hAddress = randomString(new Random().nextInt(12) + 3);
		String cAddress = randomString(new Random().nextInt(12) + 3);
		List<String> pPhones = new ArrayList<String>();
		pPhones.add(randomNumbers(8));
		pPhones.add(randomNumbers(8));
		List<String> cPhones = new ArrayList<String>();
		cPhones.add(randomNumbers(8));
		cPhones.add(randomNumbers(8));
		String homePage = randomString(new Random().nextInt(10) + 3)
				.concat(".").concat(randomString(3));
		List<String> emails = new ArrayList<String>();
		emails.add(randomString(new Random().nextInt(10) + 3).concat("@")
				.concat(randomString(new Random().nextInt(10) + 3).concat(".")
						.concat(randomString(3))));
		emails.add(randomString(new Random().nextInt(10) + 3).concat("@")
				.concat(randomString(new Random().nextInt(10) + 3).concat(".")
						.concat(randomString(3))));
		String[] photos = { "1.png", "2.png", "3.png", "4.png", "5.png" };
		String photo = photos[new Random().nextInt(5)];
		Map<String, String> others = new HashMap<String, String>();
		for (int i = 0; i < new Random().nextInt(6); i++) {
			others.put(randomString(2),
					randomString(new Random().nextInt(10) + 3));
		}
		List<String> groups = new ArrayList<String>();
		for (int i = 0; i < new Random().nextInt(6); i++) {
			groups.add(randomString(2));
		}
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		long offset = 0;
		try {
			offset = formatter.parse("01/01/2000").getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long end = 0;
		try {
			end = formatter.parse("01/01/2013").getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long diff = end - offset + 1;
		Date insertDate = new Date(offset + (long) (Math.random() * diff));
		Contact contact = new Contact(fName, lName, hAddress, cAddress,
				pPhones, cPhones, homePage, emails, photo, others, groups,
				insertDate);
		return contact;
	}

	private String makeKey(Contact contact) {
		return contact.getlName().concat(" ").concat(contact.getfName());
	}

	private String makeKey(String fName, String lName) {
		return lName.concat(" ").concat(fName);
	}

	@Override
	public synchronized void addContact(Contact contact) {
		this.contacts.put(this.makeKey(contact), contact);

	}

	@Override
	public synchronized void delContact(Contact contact) {
		this.contacts.remove(this.makeKey(contact));

	}

	@Override
	public synchronized void modifyContact(Contact sourceContact, Contact modifiedContact) {

		if (this.contacts.containsKey(this.makeKey(sourceContact))) {

			if (sourceContact.getInsertDate() == modifiedContact
					.getInsertDate()) {

				this.contacts.remove(this.makeKey(sourceContact));
				this.contacts.put(this.makeKey(modifiedContact),
						modifiedContact);
			}
		}

	}

	@Override
	public Contact getContact(String fName, String lName) {
		return this.contacts.get(this.makeKey(fName, lName));
	}

	@Override
	public Map<String, Contact> getAllContacts() {
		return new HashMap<String, Contact>(this.contacts);
	}

}
